package com.tang.qzzp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.qzzp.entity.Job;
import com.tang.qzzp.service.JobService;
import com.tang.qzzp.utils.NumberUtil;
import com.tang.qzzp.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author thq
 */
@Controller
@RequestMapping("/job")
@Slf4j(topic = "JobController")
public class JobController {
    @Resource
    private JobService jobService;

    /**
     * 获取工作
     *
     * @param pageNumber 当前页码
     * @return List
     */
    @GetMapping("/getAllJobs")
    @ResponseBody
    public List<Job> getAllJobs(@RequestParam("pageNumber") Integer pageNumber) {
        PageHelper.startPage(pageNumber, NumberUtil.DEFAUL_PAGESIZE);
        QueryWrapper<Job> wrapper = new QueryWrapper<>();
        //未被逻辑删除的Job
        wrapper.eq("job_is_deleted", 0);
        List<Job> list = jobService.list();
        return new PageInfo<>(list).getList();
    }

    /**
     * 通过JobId删除Job
     *
     * @param jobId jobId
     * @return Map
     */
    @PostMapping("/delJobById/{jobId}")
    @ResponseBody
    public Map<String, Object> delJobById(@PathVariable("jobId") String jobId) {
        Map<String, Object> resultMap = new HashMap<>(16);
        //思路 ：逻辑删除(只有管理员才能物理删除)
        QueryWrapper<Job> wrapper = new QueryWrapper<>();
        wrapper.eq("job_id", jobId);
        Job job = jobService.getOne(wrapper);
        if (job != null) {
            job.setDeleted(1);
            jobService.updateById(job);
            log.info("逻辑删除Job成功！");
            resultMap.put("result_msg", "删除成功！");
        } else {
            resultMap.put("result_msg", "已经删除了，请刷新重试！");
        }
        return resultMap;
    }

    /**
     * 添加Job
     *
     * @param request HttpServletRequest
     */
    @PostMapping("/addJob")
    @ResponseBody
    public void addJob(HttpServletRequest request) {
        JSONObject jsonObject = JSON.parseObject(request.getParameter("data"));

        Job job = new Job();
        job.setJobId(UUID.randomUUID().toString());
        job.setJobName(jsonObject.getString("jobName"));
        job.setJobDescription(jsonObject.getString("jobDescription"));
        job.setJobNeeding(jsonObject.getString("jobNeeding"));
        job.setCompanyId(jsonObject.getString("companyId"));
        job.setCreateTime(TimeUtil.getCurrentTime());
        job.setUpdateTime(TimeUtil.getCurrentTime());
        job.setDeleted(0);
        job.setVersion(0);
        job.setJobTag(Integer.valueOf(jsonObject.getString("jobTag")));

        jobService.save(job);
        log.info("添加成功！");
    }
}
